/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.search;

import br.com.deployment.daoImpl.CategoryDAOImpl;
import br.com.deployment.daoImpl.IssuesDAOImpl;
import br.com.deployment.daoImpl.OptionsDAOImpl;
import br.com.deployment.entities.Category;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.Options;
import br.com.deployment.entities.Questionnaire;
import br.com.deployment.enums.TypeIssues;
import br.com.deployment.utils.Msg;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerEditCategory implements Serializable {

    @EJB
    CategoryDAOImpl categoryService;
    @EJB
    IssuesDAOImpl issuesService;
    @EJB
    OptionsDAOImpl optionsService;
    // List
    private List<Issues> issueses;
    private List<Issues> issuesText;
    private List<Category> categorys;
    private List<Options> optiones;
    private List<Options> optionText;
    private List<Options> listOptions;
    // Objeto
    private Issues issues;
    private Category category;
    private Options options;
    private Questionnaire questionnaire;
    private Options option;
    private Category categoryCrud;
    
    private Questionnaire questionnaireViewIssues;

    public ManagerEditCategory() {
        issues = new Issues();
        issuesText = new ArrayList<Issues>();
        options = new Options();
        optiones = new ArrayList<Options>();
    }

    // Atualizando o Objeto Categoria
    public void updateCategory() {
        categoryService.update(categoryCrud);
        Msg.messagemInfo("Categoria Atualizado com Sucesso");
    }

    // Atualiza as Perguntas da Categoria 
    public void update() {
        issues.setCategory(category);
        issues.setQuestionnaire(questionnaire);
        issues.setOptions(optiones);
        issueses.add(issues);
        System.out.println(issueses);
        category.setIssueses(issueses);
        categoryService.update(category);
        Msg.messagemInfo("Categoria Atualizado com Sucesso");
        addIssues(this.questionnaire);
    }
    
    public void resgateIssues(Issues i){
        this.issues = i;
    }   
    
    public void deleteIssues(){
        issues.setActive(false);
        System.out.println(issues.isActive());
        issuesService.update(issues);
        showIssues(questionnaire);
    }

    // Retorna Issues e Joga para o TypeIssues o seu tipo;
    public Issues returnTypeIssues(Issues issues, TypeIssues type) {
        issues.setTypeIssues(type);
        return issues;
    }

    // Adciona as Alternativas da Pergunta
    public void addOptions() {
        if (options.getScore() <= 0) {
            Msg.messagemWarn("Os Pontos não podem ser Vazio");
        } else {
            optiones.add(options);
            options = new Options();
        }
    }

    // Redireciona a Página para a Escolha dos Temas
    public void viewTheme(Category cat) {
        this.category = cat;
        issues = new Issues();
        issueses = issuesService.findIssues(category);
        redirect("createTheme.xhtml");
    }

    //Redirecina para a página de Única Escolha
    public void newTypeChoceO(String string) {
        returnTypeIssues(issues, TypeIssues.CHOOSEONE);
        optiones = new ArrayList<Options>();
        redirect(string);
    }

    //Redirecina para a página de Múltipla Escolha
    public void newTypeChoiceM(String string) {
        returnTypeIssues(issues, TypeIssues.CHOICEMULTIPLE);
        optiones = new ArrayList<Options>();
        redirect(string);
    }

    //Redirecina para a página de Respostas Aberta
    public void newTypeOpen(String string) {
        returnTypeIssues(issues, TypeIssues.OPEN);
        optiones = new ArrayList<Options>();
        redirect(string);
    }

    public void getCategory(Questionnaire q) {
        categorys = categoryService.findCategoryQuestionnaire(q);
        this.questionnaireViewIssues = q;
    }

    public List<Issues> catchIssues(Category c) {
        if (c != null) {
            return issuesService.findIssuesType(c, this.questionnaireViewIssues);
        }
        return new ArrayList<Issues>();
    }

    public List<Options> catchOptionses(Issues i) {
        if (i != null) {
            return optionsService.findOptionsType(i);
        }
        return new ArrayList<Options>();
    }

    // Visualiza o Questionário, Categorias, Perguntas ...
    public void showIssues(Questionnaire q) {
        System.err.println("SHOW Q");

        this.questionnaire = q;
        // Traz a Lista de Categorias do Questionário
        categorys = categoryService.findCategoryQuestionnaire(this.questionnaire);
        for (Category c : categorys) {
            // Traz Todas as Perguntas da Categoria
            issuesText = issuesService.findIssuesType(c, this.questionnaire);
            c.setIssueses(issuesText);

            for (Issues i : c.getIssueses()) {
                // Traz Todas as Opções da Pergunta
                optionText = optionsService.findOptionsType(i);
                i.setOptions(optionText);

            }
        }
    }

    // Redireciona para a págiana de visualização do Questionário
    public void viewIssues(Questionnaire q) {
        System.err.println("CHEGOU");
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect("viewIssues.xhtml");

        } catch (IOException ex) {
            Logger.getLogger(ManagerEditCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.err.println("CHEGOU Q");
//        showIssues(q);
        getCategory(q);
    }

    // Adiciona Perguntas no Questionário
    public void addIssues(Questionnaire q) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            showIssues(q);
            context.getExternalContext().redirect("editIssues.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ManagerEditCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Redireciona a Página
    public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerEditCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    // Redireciona a Página para Atulizar a Categoria 

    public void redirectCateogry(String string, Category cat) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            categoryCrud = cat;
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerEditCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<Issues> getIssueses() {
        return issueses;
    }

    public void setIssueses(List<Issues> issueses) {
        this.issueses = issueses;
    }

    public List<Category> getCategorys() {
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    public Issues getIssues() {
        return issues;
    }

    public void setIssues(Issues issues) {
        this.issues = issues;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<Options> getOptiones() {
        return optiones;
    }

    public void setOptiones(List<Options> optiones) {
        this.optiones = optiones;
    }

    public Options getOptions() {
        return options;
    }

    public void setOptions(Options options) {
        this.options = options;
    }

    public List<Issues> getIssuesText() {
        return issuesText;
    }

    public void setIssuesText(List<Issues> issuesText) {
        this.issuesText = issuesText;
    }

    public List<Options> getOptionText() {
        return optionText;
    }

    public void setOptionText(List<Options> optionText) {
        this.optionText = optionText;
    }

    public List<Options> getListOptions() {
        return listOptions;
    }

    public void setListOptions(List<Options> listOptions) {
        this.listOptions = listOptions;
    }

    public Options getOption() {
        return option;
    }

    public void setOption(Options option) {
        this.option = option;
    }

    public Category getCategoryCrud() {
        return categoryCrud;
    }

    public void setCategoryCrud(Category categoryCrud) {
        this.categoryCrud = categoryCrud;
    }
}
